package com.scau.ums.util.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * author: 莫伟杰
 * date： 2019/8/1 11:40
 */
public class LoginInterceptor implements HandlerInterceptor {
    private String errorPage = "/WEB-INF/jsp/authorityError.jsp";
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //未登录的情况
        if(httpServletRequest.getSession().getAttribute("USER_ID")==null)
        {
            httpServletRequest.setAttribute("msg","您还未登录，请先登录！");
            httpServletRequest.setAttribute("redirect","/login");
            httpServletRequest.getRequestDispatcher(errorPage)
                    .forward(httpServletRequest,httpServletResponse);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
